package com.xinke.query.web.query.controller;

import com.xinke.query.common.result.Result;
import com.xinke.query.model.entity.GaoSuInfo;
import com.xinke.query.model.form.GaoSuInfoForm;
import com.xinke.query.model.query.FeedbackQuery;
import com.xinke.query.web.query.service.GaoSuInfoAsyncService;
import com.xinke.query.web.query.service.GaoSuInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Tag(name = "用户查询接口")
@RequestMapping("/datashare")
@Validated//开启参数校验
public class UserController {

    @Autowired
    private GaoSuInfoService gaoSuInfoService;

    @Autowired
    private GaoSuInfoAsyncService gaoSuInfoAsyncService;


    //用户端调用接口
    @Operation(summary = "查询高速卡口/出入口记录")
    @PostMapping("/queryPassData")
    public Result<List<GaoSuInfo>> queryPassData(@Valid @RequestBody GaoSuInfoForm gaoSuInfoQuery) {
        gaoSuInfoAsyncService.insertQuery(gaoSuInfoQuery);
        return Result.build(null, 200, "数据接收成功");
    }

    @Operation(summary = "查询ETC登记信息")
    @PostMapping("/queryEtcInfo")
    public Result<List<GaoSuInfo>> queryEtcInfo(@Valid @RequestBody GaoSuInfoForm gaoSuInfoQuery) {
        gaoSuInfoAsyncService.insertQuery(gaoSuInfoQuery);
        return Result.build(null, 200, "数据接收成功");

    }

    @Operation(summary = "查询高速卡口/出入口图片信息")
    @PostMapping("/queryPassPicData")
    public Result<List<GaoSuInfo>> queryPassPicData(@Valid @RequestBody GaoSuInfoForm gaoSuInfoQuery) {
        gaoSuInfoAsyncService.insertQuery(gaoSuInfoQuery);
        return Result.build(null, 200, "数据接收成功");
    }

    @Operation(summary = "数据查询申请结果下载接口")
    @PostMapping("/feedbackData")
    public ResponseEntity<FileSystemResource> feedbackData(@Valid @RequestBody FeedbackQuery query) {
        //返回.zip文件
        File zipFile = gaoSuInfoService.feedbackData(query);
        //设置响应头
        HttpHeaders headers = new HttpHeaders();
        //处理文件名
        String fileName = zipFile.getName();
        String encodingFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replace("+", "%20");
        headers.add("Content-Disposition", "attachment; filename=\"" + encodingFileName + "\"");
        headers.add("Content-Type", "application/zip");
        //返回文件资源
        FileSystemResource resource = new FileSystemResource(zipFile);
        return new ResponseEntity<>(resource, headers, HttpStatus.OK);
    }
}
